package cate.game.pvp.minecraft;

import cate.game.framework.ModuleMan;
import cate.game.framework.ThreadShare;
import cate.game.pvp.minecraft.MinecraftMessagingModule.*;
import cate.game.pvp.minecraft.data.MinecraftPvpRecord;
import cate.game.pvp.minecraft.event.MinecraftNotificationEvent;
import cate.game.pvp.minecraft.event.MinecraftStatusEvent;
import cate.game.role.Role;
import cate.game.role.pvp.minecraft.msg.MinecraftMineAreaUpdateSinglePointResp;
import cp.solution.rpc.RpcMsgHead;
import easy.java.dev.note.NoteClass;

@NoteClass("灵能矿洞-Game接收消息模块")
public class MinecraftReceiveModule extends ModuleMan {
    @Override
    public void initDependent() throws Exception {
        game.rpcRecv().onFireAndForgetTemplate(MinecraftInnerMsg.MC_GAME_CONNECT_BACK, CrossMineAreaState.class, this::onGameConnectBack);
        game.rpcRecv().onFireAndForgetTemplate(MinecraftInnerMsg.MC_SYNC_USED_INFO, CrossRoleUsedInfo.class, this::onSyncUsedInfo);
        game.rpcRecv().onFireAndForgetTemplate(MinecraftInnerMsg.MC_OVERVIEW_BACK, CrossOverviewBack.class, this::onOverviewBack);
        game.rpcRecv().onFireAndForgetTemplate(MinecraftInnerMsg.MC_VIEW_LAYER_BACK, CrossViewLayerBack.class, this::onViewLayerBack);
        game.rpcRecv().onFireAndForgetTemplate(MinecraftInnerMsg.MC_VIEW_MY_POINTS_BACK, CrossViewMyPointsBack.class, this::onViewMyPointsBack);
        game.rpcRecv().onFireAndForgetTemplate(MinecraftInnerMsg.MC_VIEW_POINT_DETAIL_BACK, CrossViewPointDetailBack.class, this::onViewPointDetailBack);
        game.rpcRecv().onFireAndForgetTemplate(MinecraftInnerMsg.MC_TRY_FIGHT_BACK, CrossTryFightPointBack.class, this::onTryFightBack);
        game.rpcRecv().onFireAndForgetTemplate(MinecraftInnerMsg.MC_FIGHT_RESULT_REPORT_BACK, CrossFightResultReportBack.class, this::onFightResultReportBack);
        game.rpcRecv().onFireAndForgetTemplate(MinecraftInnerMsg.MC_GET_HANG_REWARD_BACK, CrossGetHangRewardBack.class, this::onGetHangRewardBack);
        game.rpcRecv().onFireAndForgetTemplate(MinecraftInnerMsg.MC_PROTECT_POINT_BACK, CrossProtectPointBack.class, this::onProtectPointBack);
        game.rpcRecv().onFireAndForgetTemplate(MinecraftInnerMsg.MC_GIVE_UP_POINT_BACK, CrossGiveUpPointBack.class, this::onGiveUpPointBack);
        game.rpcRecv().onFireAndForgetTemplate(MinecraftInnerMsg.MC_TRANSFER_PVP_RECORD, MinecraftPvpRecord.class, this::onTransferPvpRecord);
        // 事件
        game.rpcRecv().onFireAndForgetTemplate(MinecraftInnerMsg.MC_STATUS_EVENT, MinecraftStatusEvent.class, this::onStatusEvent);
        game.rpcRecv().onFireAndForgetTemplate(MinecraftInnerMsg.MC_NOTIFICATION_EVENT, MinecraftNotificationEvent.class, this::onNotificationEvent);
    }

    /**
     * 点位通知事件
     *
     * @param head  头信息
     * @param event 事件
     */
    private void onNotificationEvent(RpcMsgHead head, MinecraftNotificationEvent event) {
        if (event.getVo() == null) {
            return;
        }
        MinecraftMineAreaUpdateSinglePointResp resp = new MinecraftMineAreaUpdateSinglePointResp(event.getVo());
        // 仅有关注当前层的玩家 和占领点位的玩家 会发送变化数据
        game.role.activeForOnline(role -> {
            if (role.getPvp().minecraft.gatherPO.watchingLayer == event.getVo().layer) {
                role.sendNow(resp);
            } else if (event.getVo().guardPlayer != null
                    && role.getUid().equals(event.getVo().guardPlayer.uid)) {
                role.sendNow(resp);
            }
        });
    }

    /**
     * 活动状态事件
     *
     * @param head  头信息
     * @param event 事件
     */
    private void onStatusEvent(RpcMsgHead head, MinecraftStatusEvent event) {
        game.pvp.minecraft.updateMinecraftClosed(event.isClose(), event.getOpenTime(), event.getCloseTime());
    }

    /**
     * 游戏服连接返回
     *
     * @param head  头信息
     * @param state 活动状态
     */
    public void onGameConnectBack(RpcMsgHead head, CrossMineAreaState state) {
        game.pvp.minecraft.updateMinecraftClosed(state.close, state.openTime, state.closeTime);
    }

    /**
     * 同步玩家阵容占用信息
     *
     * @param head 头信息
     * @param info 占用信息
     */
    public void onSyncUsedInfo(RpcMsgHead head, CrossRoleUsedInfo info) {
        Role role = game.role.getRole(info.roleUid);
        if (role != null) {
            ThreadShare.instance().exe(role, () -> role.getPvp().minecraft.gatherPO.updateUsedHeroProtoIds(info.usedHallows,
                    info.usedHeroProtoIds));
        }
    }

    /**
     * 矿区概览信息返回
     *
     * @param head    头信息
     * @param message 返回消息
     */
    public void onOverviewBack(RpcMsgHead head, CrossOverviewBack message) {
        Role role = game.role.getRole(message.roleUid);
        if (role != null && role.getStatus().isOnline()) {
            ThreadShare.instance().exe(role, () -> role.sendNow(message.resp));
        }
    }

    /**
     * 矿区单层信息返回
     *
     * @param head    头信息
     * @param message 返回消息
     */
    public void onViewLayerBack(RpcMsgHead head, CrossViewLayerBack message) {
        Role role = game.role.getRole(message.roleUid);
        if (role != null && role.getStatus().isOnline()) {
            ThreadShare.instance().exe(role, () -> {
                if (message.result.ok()) {
                    role.sendNow(message.result.data);
                } else {
                    game.notice.message(role, message.result);
                }
            });
        }
    }

    /**
     * 查看我的矿点信息返回
     *
     * @param head    头信息
     * @param message 返回消息
     */
    public void onViewMyPointsBack(RpcMsgHead head, CrossViewMyPointsBack message) {
        Role role = game.role.getRole(message.roleUid);
        if (role != null && role.getStatus().isOnline()) {
            ThreadShare.instance().exe(role, () -> role.sendNow(message.resp));
        }
    }

    /**
     * 查看矿点详情信息返回
     *
     * @param head    头信息
     * @param message 返回消息
     */
    public void onViewPointDetailBack(RpcMsgHead head, CrossViewPointDetailBack message) {
        Role role = game.role.getRole(message.roleUid);
        if (role != null && role.getStatus().isOnline()) {
            ThreadShare.instance().exe(role, () -> {
                if (!message.result.ok()) {
                    game.notice.message(role, message.result);
                } else {
                    role.sendNow(message.result.data);
                }
            });
        }
    }

    /**
     * 尝试发起战斗返回
     *
     * @param head    头信息
     * @param message 返回消息
     */
    public void onTryFightBack(RpcMsgHead head, CrossTryFightPointBack message) {
        Role role = game.role.getRole(message.roleUid);
        if (role != null) {
            ThreadShare.instance().exe(role, () -> role.getPvp().minecraft.gatherPO.onCrossTryFightBack(message));
        }
    }

    /**
     * 战斗结果上报返回
     *
     * @param head    头信息
     * @param message 返回消息
     */
    public void onFightResultReportBack(RpcMsgHead head, CrossFightResultReportBack message) {
        Role role = game.role.getRole(message.roleUid);
        if (role != null) {
            ThreadShare.instance().exe(role, () ->
                    role.getPvp().minecraft.gatherPO.onCrossBackFightResultResp(message.fightType, message.lostRes, message.protectTime, message.r));
        }
    }

    /**
     * 领取挂机奖励返回
     *
     * @param head    头信息
     * @param message 返回消息
     */
    public void onGetHangRewardBack(RpcMsgHead head, CrossGetHangRewardBack message) {
        Role role = game.role.getRole(message.roleUid);
        if (role != null) {
            ThreadShare.instance().exe(role, () -> role.getPvp().minecraft.gatherPO.onCrossBackGetHangReward(message.result));
        }
    }

    /**
     * 保护矿点返回
     *
     * @param head    头信息
     * @param message 返回消息
     */
    public void onProtectPointBack(RpcMsgHead head, CrossProtectPointBack message) {
        Role role = game.role.getRole(message.roleUid);
        if (role != null) {
            ThreadShare.instance().exe(role,
                    () -> role.getPvp().minecraft.gatherPO.onCrossProtectPointBack(message.result));
        }
    }

    /**
     * 放弃矿点返回
     *
     * @param head    头信息
     * @param message 返回消息
     */
    public void onGiveUpPointBack(RpcMsgHead head, CrossGiveUpPointBack message) {
        Role role = game.role.getRole(message.roleUid);
        if (role != null) {
            ThreadShare.instance().exe(role,
                    () -> role.getPvp().minecraft.gatherPO.onCrossGiveUpPointBack(message.result));
        }
    }

    /**
     * 矿点战报传输
     *
     * @param head   头信息
     * @param record 战报
     */
    public void onTransferPvpRecord(RpcMsgHead head, MinecraftPvpRecord record) {
        Role role = game.role.getRole(record.snapshotB.uid);
        if (role != null) {
            ThreadShare.instance().exe(role, () -> role.getPvp().minecraft.gatherPO.addPvpRecord(record));
        }
    }
}
